<?php

namespace App\Service;

use App\Model\CommunityTopicModel;
use App\Model\ConfigCityModel;
use App\Model\ConfigHouseModel;
use App\Model\ConfigParkModel;
use App\Model\ConfigRentModel;
use App\Model\ConfigRepairModel;
use App\Model\ConfigShareItemModel;
use App\Model\ConfigSystemModel;
use App\Model\ShareTopicModel;
use App\Model\StaffModel;
use App\Model\UserHouseModel;
use App\Model\UserModel;
use EasySwoole\Mysqli\Exception\Exception;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Db\Cursor;
use EasySwoole\ORM\Db\CursorInterface;
use Throwable;

class ConfigService extends BaseService
{
    /**
     * 获取系统配置
     * @param $name
     * @param $type
     * @return mixed|null
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getSys($name, $type)
    {
        $config = ConfigSystemModel::create()->get(['name' => $name, 'type' => $type]);
        if (!$config) return null;
        return $config->value;
    }

    /**
     * 获取房间信息
     * @param $village
     * @param $unit
     * @param $floor
     * @param $house
     * @return string
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getHouse($village, $unit, $floor, $house): string
    {
        $village1 = ConfigHouseModel::create()->getConfig(['id' => $village]);
        if (!$village1) {
            return '-';
        }
        $unit1 = ConfigHouseModel::create()->getConfig(['id' => $unit]);
        if (!$unit1) {
            return '-';
        }
        $floor1 = ConfigHouseModel::create()->getConfig(['id' => $floor]);
        if (!$floor1) {
            return '-';
        }
        $house1 = ConfigHouseModel::create()->getConfig(['id' => $house]);
        if (!$house1) {
            return '-';
        }

        return $village1->name . $unit1->name . $floor1->name . $house1->name;
    }

    /**
     * 获取住户类型
     * @param $type
     * @return string
     */
    public function houseType($type): string
    {
        switch ($type) {
            case 0:
                $label = '业主';
                break;
            case 1:
                $label = '家人';
                break;
            case 2:
                $label = '租户';
                break;
            default:
                $label = '游客';
                break;
        }

        return $label;
    }

    /**
     * 获取车位
     * @param $park
     * @return mixed|string|null
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getPark($park)
    {
        $data = ConfigParkModel::create()->getConfig(['id' => $park]);
        if (!$data) return '-';
        return $data->name;
    }

    /**
     * 访客状态
     * @param $status
     * @return string
     */
    public function getVisitStatus($status): string
    {
        switch ($status) {
            case 0:
                $label = '待处理';
                break;
            case 1:
                $label = '已处理';
                break;
            case -1:
                $label = '未放行';
                break;
            default:
                $label = '未知';
                break;
        }

        return $label;
    }

    /**
     * 报修状态
     * @param $status
     * @return string
     */
    public function getRepairStatus($status): string
    {
        switch ($status) {
            case 0:
                $label = '待处理';
                break;
            case 1:
                $label = '已处理';
                break;
            case 2:
                $label = '处理中';
                break;
            case -1:
                $label = '已关闭';
                break;
            default:
                $label = '未知';
                break;
        }

        return $label;
    }

    /**
     * @param $uid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getUserInfo($uid): array
    {
        $user = UserHouseModel::create()->getUser(['uid' => $uid]);
        $name = $user->name ?? '';
        $phone = UserModel::create()->getUser(['id' => $uid]);
        $tel = $phone->phone ?? '';

        return [
            'u_name' => $name,
            'u_tel' => $tel
        ];
    }

    /**
     * @param $sid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getStaffInfo($sid): array
    {
        $user = StaffModel::create()->getUser(['id' => $sid]);
        $name = $user->name ?? '';
        $tel = $user->phone ?? '';

        return [
            'u_name' => $name,
            'u_tel' => $tel
        ];
    }

    /**
     * 获取维修类型
     * @param $type
     * @return mixed|string
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getRepairType($type)
    {
        $re = ConfigRepairModel::create()->getConfig(['id' => $type]);
        return $re->name ?? '其他';
    }

    /**
     * 获取车辆类型
     * @param $type
     * @return string
     */
    public function carType($type): string
    {
        switch ($type) {
            case 0:
                $label = '小型车';
                break;
            case 1:
                $label = '中型车';
                break;
            case 2:
                $label = '大型车';
                break;
            default:
                $label = '';
                break;
        }

        return $label;
    }

    /**
     * @param $status
     * @return string
     */
    public function getCarStatus($status): string
    {
        switch ($status) {
            case 0:
                $label = '待处理';
                break;
            case 1:
                $label = '审核成功';
                break;
            case -1:
                $label = '审核失败';
                break;
            default:
                $label = '未知';
                break;
        }

        return $label;
    }

    /**
     * 获取话题
     * @param $tid
     * @return string
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getTopic($tid): string
    {
        $theme = CommunityTopicModel::create()->getConfig(['id' => $tid]);
        if (!$theme) return "";
        return "# " . $theme->name . " #";
    }

    /**
     * 获取话题
     * @param $tid
     * @return string
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getLatticeTopic($tid): string
    {
        $theme = ShareTopicModel::create()->getConfig(['id' => $tid]);
        if (!$theme) return "";
        return "# " . $theme->name . " #";
    }

    /**
     * 共享状态
     * @param $status
     * @return string
     */
    public function getShareStatus($status): string
    {
        switch ($status) {
            case 0:
                $label = '待审核';
                break;
            case 1:
                $label = '进行中';
                break;
            case 2:
                $label = '待支付';
                break;
            case -1:
                $label = '已取消';
                break;
            default:
                $label = '';
                break;
        }

        return $label;
    }

    /**
     * @param $nid
     * @return ConfigShareItemModel|array|bool|AbstractModel|Cursor|CursorInterface|null
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getServiceItem($nid)
    {
        return ConfigShareItemModel::create()->getConfig(['id' => $nid]);
    }

    /**
     * 工具状态
     * @param $status
     * @return string
     */
    public function getToolsStatus($status): string
    {
        switch ($status) {
            case 0:
                $label = '待审核';
                break;
            case 1:
                $label = '借用中';
                break;
            case 2:
                $label = '待使用';
                break;
            case 3:
                $label = '已归还';
                break;
            case -1:
                $label = '过期/取消';
                break;
            default:
                $label = '';
                break;
        }

        return $label;
    }

    /**
     * 获取订单状态
     * @param $status
     * @return string
     */
    public function getOrderStatus($status): string
    {
        switch ($status) {
            case 0:
                $label = '已取消';
                break;
            case 1:
                $label = '预订单';
                break;
            case 2:
                $label = '待支付';
                break;
            case 3:
                $label = '支付中';
                break;
            case 4:
                $label = '已支付';
                break;
            case 5:
                $label = '已发货';
                break;
            case 6:
                $label = '已完成';
                break;
            default:
                $label = '';
                break;
        }

        return $label;
    }

    /**
     * @param $unit
     * @return string
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getKeeperA($unit): string
    {
        $h = ConfigHouseModel::create()->getConfig(['id' => $unit]);
        if (!$h) return '物业热线';

        $h1 = ConfigHouseModel::create()->getConfig(['id' => $h->nid]);
        if (!$h1) return '物业热线' . $h->name;
        return $h1->name . $h->name;
    }

    /**
     * @param $value
     * @return mixed|null
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     * @throws Exception
     */
    public function getCity($value)
    {
        $city = ConfigCityModel::create()->getCity(['value' => $value]);
        if (!$city) {
            return '未知';
        }
        return $city->label;
    }

    public function getRentType($type)
    {
        $label = '未知';

        $l = ConfigRentModel::create()->getConfig(['id' => $type]);
        if ($l) $label = $l->label;

        return $label;
    }
}